﻿#include "function_blob_match.h"


FunctionBlobMatch::FunctionBlobMatch(QDialog* parent, QSettings* qsettings , QString section) : QDialog(parent)
{

    m_qsettings = qsettings;
    m_section   = section;
    //m_model_image_path = QCoreApplication::applicationDirPath() + "/" + m_model_image_path;

    this->setWindowTitle(("电极提取和定位"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    setFixedSize(1920*ratio_x,1010*ratio_y);

    m_view = new MyQGraphicsView(this);
    m_view->setGeometry(660 * ratio_x, 0, 1000 * 1.25 * ratio_x, 800 * 1.25 * ratio_y);



    m_view->m_graphics_shape_rect = new GraphicsShapeRect(100,100,100,100);
    m_view->m_scene->addItem(m_view->m_graphics_shape_rect);
    QRectF qrectf = m_view->m_graphics_shape_rect->boundingRect();
    row1 = (int)qrectf.y();
    column1 = (int)qrectf.x();
    row2 = (int)(qrectf.y() + qrectf.height());
    column2 = (int)(qrectf.x() + qrectf.width());


    
    connect(m_view->m_graphics_shape_rect, &GraphicsShapeRect::sendMessage, m_view->m_pixmapItem, &MyQGraphicsPixmapItem::receiveMessage);

    QSize control_size(120*ratio_x,40*ratio_y);

    QWidget* p = new QWidget(this);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(150 * ratio_x, 40 * ratio_y);


    m_qlabel_threshold_low = new QLabel();
    m_qlabel_threshold_low->setText("灰度阈值最小值");
    m_qlabel_threshold_low->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_threshold_low,index,0);
    index++;

    m_qspinbox_threshold_low = new QSpinBox();
    m_qspinbox_threshold_low->setFixedSize(control_size);
    m_qspinbox_threshold_low->setMinimum(0);
    m_qspinbox_threshold_low->setMaximum(255);
	m_qspinbox_threshold_low->setReadOnly(true);
    qgridLayout->addWidget(m_qspinbox_threshold_low,index,0);
    
    m_qslider_threshold_low = new QSlider(Qt::Horizontal);
    m_qslider_threshold_low->setTickPosition(QSlider::NoTicks);
    m_qslider_threshold_low->setSingleStep(1);
    m_qslider_threshold_low->setMinimum(0);
    m_qslider_threshold_low->setMaximum(255);
    
    qgridLayout->addWidget(m_qslider_threshold_low,index,1);
    index++;

    m_qlabel_threshold_high = new QLabel();
    m_qlabel_threshold_high->setText("灰度阈值最大值");
    m_qlabel_threshold_high->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_threshold_high,index,0);
    index++;

    m_qspinbox_threshold_high = new QSpinBox();
    m_qspinbox_threshold_high->setFixedSize(control_size);
    m_qspinbox_threshold_high->setMinimum(0);
    m_qspinbox_threshold_high->setMaximum(255);
	m_qspinbox_threshold_high->setReadOnly(true);
    qgridLayout->addWidget(m_qspinbox_threshold_high,index,0);
    
    m_qslider_threshold_high = new QSlider(Qt::Horizontal);
    m_qslider_threshold_high->setTickPosition(QSlider::NoTicks);
    m_qslider_threshold_high->setSingleStep(1);
    m_qslider_threshold_high->setMinimum(0);
    m_qslider_threshold_high->setMaximum(255);
    
    qgridLayout->addWidget(m_qslider_threshold_high,index,1);
    index++;


    m_qlabel_area_low = new QLabel();
    m_qlabel_area_low->setText("电极面积下限");
    m_qlabel_area_low->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_area_low,index,0);

    m_qspinbox_area_low = new QSpinBox();
    m_qspinbox_area_low->setFixedSize(control_size);
    m_qspinbox_area_low->setMinimum(10);
    m_qspinbox_area_low->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_area_low,index,1);

    index++;

    m_qlabel_area_high = new QLabel();
    m_qlabel_area_high->setText("电极面积上限");
    m_qlabel_area_high->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_area_high,index,0);


    m_qspinbox_area_high = new QSpinBox();
    m_qspinbox_area_high->setFixedSize(control_size);
    m_qspinbox_area_high->setMinimum(10);
    m_qspinbox_area_high->setMaximum(100000);
    qgridLayout->addWidget(m_qspinbox_area_high,index,1);

    index++;
  
    m_qlabel_height_low = new QLabel();
    m_qlabel_height_low->setText("电极高度下限");
    m_qlabel_height_low->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_height_low,index,0);

    m_qspinbox_height_low = new QSpinBox();
    m_qspinbox_height_low->setFixedSize(control_size);
    m_qspinbox_height_low->setMinimum(10);
    m_qspinbox_height_low->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_height_low,index,1);

    index++;

    m_qlabel_height_high = new QLabel();
    m_qlabel_height_high->setText("电极高度上限");
    m_qlabel_height_high->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_height_high,index,0);


    m_qspinbox_height_high = new QSpinBox();
    m_qspinbox_height_high->setFixedSize(control_size);
    m_qspinbox_height_high->setMinimum(10);
    m_qspinbox_height_high->setMaximum(100000);
    qgridLayout->addWidget(m_qspinbox_height_high,index,1);

    index++;
	m_qlabel_width_low = new QLabel();
	m_qlabel_width_low->setText("电极宽度下限");
	m_qlabel_width_low->setFixedSize(control_size);
	qgridLayout->addWidget(m_qlabel_width_low, index, 0);

	m_qspinbox_width_low = new QSpinBox();
	m_qspinbox_width_low->setFixedSize(control_size);
	m_qspinbox_width_low->setMinimum(10);
	m_qspinbox_width_low->setMaximum(10000);
	qgridLayout->addWidget(m_qspinbox_width_low, index, 1);

	index++;

	m_qlabel_width_high = new QLabel();
	m_qlabel_width_high->setText("电极宽度上限");
	m_qlabel_width_high->setFixedSize(control_size);
	qgridLayout->addWidget(m_qlabel_width_high, index, 0);


	m_qspinbox_width_high = new QSpinBox();
	m_qspinbox_width_high->setFixedSize(control_size);
	m_qspinbox_width_high->setMinimum(10);
	m_qspinbox_width_high->setMaximum(100000);
	qgridLayout->addWidget(m_qspinbox_width_high, index, 1);

	index++;

	m_qlabel_mask_column1 = new QLabel();
	m_qlabel_mask_column1->setText("屏蔽区间x1");
	m_qlabel_mask_column1->setFixedSize(control_size);
	qgridLayout->addWidget(m_qlabel_mask_column1, index, 0);


	m_qspinbox_mask_column1 = new QSpinBox();
	m_qspinbox_mask_column1->setFixedSize(control_size);
	m_qspinbox_mask_column1->setMinimum(0);
	m_qspinbox_mask_column1->setMaximum(3000);
	qgridLayout->addWidget(m_qspinbox_mask_column1, index, 1);

	index++;

	m_qlabel_mask_column2 = new QLabel();
	m_qlabel_mask_column2->setText("屏蔽区间x2");
	m_qlabel_mask_column2->setFixedSize(control_size);
	qgridLayout->addWidget(m_qlabel_mask_column2, index, 0);


	m_qspinbox_mask_column2 = new QSpinBox();
	m_qspinbox_mask_column2->setFixedSize(control_size);
	m_qspinbox_mask_column2->setMinimum(0);
	m_qspinbox_mask_column2->setMaximum(3000);
	qgridLayout->addWidget(m_qspinbox_mask_column2, index, 1);

    index++;

    m_qlabel_closing_height = new QLabel();
    m_qlabel_closing_height->setText("电极高度方向膨胀");
    m_qlabel_closing_height->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_closing_height, index, 0);


    m_qspinbox_closing_height = new QSpinBox();
    m_qspinbox_closing_height->setFixedSize(control_size);
    m_qspinbox_closing_height->setMinimum(1);
    m_qspinbox_closing_height->setMaximum(49);
    qgridLayout->addWidget(m_qspinbox_closing_height, index, 1);

	index++;
    m_qcombobox_first_black = new QComboBox();
    m_qcombobox_first_black->setFixedSize(control_size);
    m_qcombobox_first_black->addItem("不启动纸袋方孔");
    m_qcombobox_first_black->addItem("启动纸袋方孔");
    qgridLayout->addWidget(m_qcombobox_first_black, index, 0);
    index++;

    m_qlabel_first_black_erosion = new QLabel();
    m_qlabel_first_black_erosion->setText("纸袋方孔边缘过滤尺寸");
    m_qlabel_first_black_erosion->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_first_black_erosion,index,0);
    index++;

    m_qspinbox_first_black_erosion = new QSpinBox();
    m_qspinbox_first_black_erosion->setFixedSize(control_size);
    m_qspinbox_first_black_erosion->setMinimum(1);
    m_qspinbox_first_black_erosion->setMaximum(20);
    m_qspinbox_threshold_high->setReadOnly(true);
    qgridLayout->addWidget(m_qspinbox_first_black_erosion,index,0);
    m_qslider_first_black_erosion = new QSlider(Qt::Horizontal);
    m_qslider_first_black_erosion->setTickPosition(QSlider::NoTicks);
    m_qslider_first_black_erosion->setSingleStep(1);
    m_qslider_first_black_erosion->setMinimum(1);
    m_qslider_first_black_erosion->setMaximum(20);

    qgridLayout->addWidget(m_qslider_first_black_erosion,index,1);



    index++;


    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(control_size);
    m_pushbutton_save_parameter->setText("保存参数");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &FunctionBlobMatch::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);
    
    m_pushbutton_create_model = new QPushButton(this);
    m_pushbutton_create_model->setFixedSize(control_size);
    m_pushbutton_create_model->setText("创建模型");
    connect(m_pushbutton_create_model, &QPushButton::clicked, this, &FunctionBlobMatch::create_model);
    qgridLayout->addWidget(m_pushbutton_create_model, index,1);
    index++;

    m_pushbutton_show_orign_image = new QPushButton(this);
    m_pushbutton_show_orign_image->setFixedSize(control_size);
    m_pushbutton_show_orign_image->setText("显示原图");
    connect(m_pushbutton_show_orign_image, &QPushButton::clicked, this,[=]() {
        QImage q_image = matToQImage(m_image);
        m_view->setImage(q_image,"");
        return true;
    });
    qgridLayout->addWidget(m_pushbutton_show_orign_image, index,0);

    m_pushbutton_detection = new QPushButton(this);
    m_pushbutton_detection->setFixedSize(control_size);
    m_pushbutton_detection->setText("检测");
    connect(m_pushbutton_detection, &QPushButton::clicked, this, &FunctionBlobMatch::detectionImage);
    qgridLayout->addWidget(m_pushbutton_detection, index,1);
    
    index++;
    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(control_size);
    m_pushbutton_return->setText("退出");
    qgridLayout->addWidget(m_pushbutton_return, index, 0);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });

    
    try
    {

        m_proc_createBlobMatch = new HDevProcedure("createBlobMatch");
        m_call_createBlobMatch = new HDevProcedureCall(*m_proc_createBlobMatch);

        m_proc_blobMatch = new HDevProcedure("blobMatch");
        m_call_blobMatch = new HDevProcedureCall(*m_proc_blobMatch);
    }
    catch (HDevEngineException& hdev_exception)
    {
        //QMessageBox::information(nullptr, m_path_para, hdev_exception.Message());
    }


    loadParameter(m_path_para,m_section);

	m_qslider_threshold_low->setValue(m_qspinbox_threshold_low->value());
	connect(m_qslider_threshold_low, &QSlider::valueChanged, this, [=](int value) {
		
		if (m_qspinbox_threshold_high->value() <= m_qspinbox_threshold_low->value())
		{
			m_qspinbox_threshold_low->setValue(m_qspinbox_threshold_high->value() - 1);
			m_qslider_threshold_low->setValue(m_qspinbox_threshold_high->value() - 1);

		}
		else
		{
			m_qspinbox_threshold_low->setValue(value);
		}
        testImage();

	});
	m_qslider_threshold_high->setValue(m_qspinbox_threshold_high->value());
	connect(m_qslider_threshold_high, &QSlider::valueChanged, this, [=](int value) {
		//m_qspinbox_threshold_high->setValue(value);
		if (m_qspinbox_threshold_high->value() <= m_qspinbox_threshold_low->value())
		{
			m_qspinbox_threshold_high->setValue(m_qspinbox_threshold_low->value() + 1);
			m_qslider_threshold_high->setValue(m_qspinbox_threshold_low->value() + 1);
			return;
		}
		else
		{
			m_qspinbox_threshold_high->setValue(value);
		}

        testImage();


	});

    m_qslider_first_black_erosion->setValue(m_qspinbox_first_black_erosion->value());
    connect(m_qslider_first_black_erosion, &QSlider::valueChanged, this, [=](int value) {
            m_qspinbox_first_black_erosion->setValue(value);
            QString message;

            if(m_image.empty())
            {
                message += "创建Blob匹配模板失败，模板图像为空 ";
                return ;
            }
            m_image_show = m_image.clone();
            try
            {
                HObject image = matToHImage(m_image_show);

                // Set the input parameters of the procedure
                m_call_createBlobMatch->SetInputIconicParamObject(1, image);
                m_call_createBlobMatch->SetInputCtrlParamTuple("row1", row1);
                m_call_createBlobMatch->SetInputCtrlParamTuple("column1", column1);
                m_call_createBlobMatch->SetInputCtrlParamTuple("row2", row2);
                m_call_createBlobMatch->SetInputCtrlParamTuple("column2", column2);
                m_call_createBlobMatch->SetInputCtrlParamTuple("threshold_low", m_qspinbox_threshold_low->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("threshold_high",  m_qspinbox_threshold_high->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("area_low",  m_qspinbox_area_low->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("area_high",m_qspinbox_area_high->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("height_low",  m_qspinbox_height_low->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("height_high",m_qspinbox_height_high->value());
				m_call_createBlobMatch->SetInputCtrlParamTuple("width_low", m_qspinbox_width_low->value());
				m_call_createBlobMatch->SetInputCtrlParamTuple("width_high", m_qspinbox_width_high->value());
				m_call_createBlobMatch->SetInputCtrlParamTuple("mask_column1", m_qspinbox_mask_column1->value());
				m_call_createBlobMatch->SetInputCtrlParamTuple("mask_column2", m_qspinbox_mask_column2->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("closing_height", m_qspinbox_closing_height->value());
                m_call_createBlobMatch->SetInputCtrlParamTuple("first_black", first_black);
                m_call_createBlobMatch->SetInputCtrlParamTuple("first_black_erosion_x", 1);
                m_call_createBlobMatch->SetInputCtrlParamTuple("first_black_erosion_y", m_qspinbox_first_black_erosion->value());
                // Execute the procedure


                //m_call_createBlobMatch->SetWaitForDebugConnection(true);
                m_call_createBlobMatch->Execute();




                // get the output parameters
                HTuple ret = m_call_createBlobMatch->GetOutputCtrlParamTuple(1);
                HTuple msg = m_call_createBlobMatch->GetOutputCtrlParamTuple(2);
                HTuple model_row = m_call_createBlobMatch->GetOutputCtrlParamTuple(3);
                HTuple model_column = m_call_createBlobMatch->GetOutputCtrlParamTuple(4);
                HTuple model_phi = m_call_createBlobMatch->GetOutputCtrlParamTuple(5);
                HTuple model_length1 = m_call_createBlobMatch->GetOutputCtrlParamTuple(6);
                HTuple model_length2 = m_call_createBlobMatch->GetOutputCtrlParamTuple(7);

                HObject region = m_call_createBlobMatch->GetOutputIconicParamObject("region");
                HObject region_erosion = m_call_createBlobMatch->GetOutputIconicParamObject("region_erosion");

                m_image_show = drawRegion(m_image_show, region_erosion, Scalar(0, 0, 0));

				
				
				

                QImage q_image = matToQImage(m_image_show);
                m_view->setImage(q_image, message);
                m_view->setRectPose(row1, column1, row2, column2);
                return ;
            }
            catch (HDevEngineException& hdev_exception)
            {
                HDevEngine().SetHDevOperatorImpl(NULL);

                //QMessageBox::information(nullptr, m_path_para, hdev_exception.Message());
            }


            return ;

    });


}

FunctionBlobMatch::~FunctionBlobMatch()
{
    ;
}

void FunctionBlobMatch::create_model()
{
    QString message;

    if(m_image.empty())
    {
        message += "创建Blob匹配模板失败，模板图像为空 ";
        //QMessageBox::information(nullptr, "", message);
        return ;
    }
    m_image_show = m_image.clone();
    try
    {
        HObject image = matToHImage(m_image_show);

        // Set the input parameters of the procedure
        m_call_createBlobMatch->SetInputIconicParamObject(1, image);
        m_call_createBlobMatch->SetInputCtrlParamTuple("row1", row1);
        m_call_createBlobMatch->SetInputCtrlParamTuple("column1", column1);
        m_call_createBlobMatch->SetInputCtrlParamTuple("row2", row2);
        m_call_createBlobMatch->SetInputCtrlParamTuple("column2", column2);
        m_call_createBlobMatch->SetInputCtrlParamTuple("threshold_low", m_qspinbox_threshold_low->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("threshold_high",  m_qspinbox_threshold_high->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("area_low",  m_qspinbox_area_low->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("area_high",m_qspinbox_area_high->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("height_low",  m_qspinbox_height_low->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("height_high",m_qspinbox_height_high->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("width_low", m_qspinbox_width_low->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("width_high", m_qspinbox_width_high->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("mask_column1", m_qspinbox_mask_column1->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("mask_column2", m_qspinbox_mask_column2->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("closing_height", m_qspinbox_closing_height->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("first_black", first_black);
        m_call_createBlobMatch->SetInputCtrlParamTuple("first_black_erosion_x", 1);
        m_call_createBlobMatch->SetInputCtrlParamTuple("first_black_erosion_y", m_qspinbox_first_black_erosion->value());
        // Execute the procedure

        
        
        m_call_createBlobMatch->Execute();
        
		/*HDevEngine().StartDebugServer();
		m_call_createBlobMatch->SetWaitForDebugConnection(true);
		m_call_createBlobMatch->Execute();
		HDevEngine().StopDebugServer();*/


        // get the output parameters
        HTuple ret = m_call_createBlobMatch->GetOutputCtrlParamTuple(1);
        HTuple msg = m_call_createBlobMatch->GetOutputCtrlParamTuple(2);
        HTuple model_row = m_call_createBlobMatch->GetOutputCtrlParamTuple(3);
        HTuple model_column = m_call_createBlobMatch->GetOutputCtrlParamTuple(4);
        HTuple model_phi = m_call_createBlobMatch->GetOutputCtrlParamTuple(5);
        HTuple model_length1 = m_call_createBlobMatch->GetOutputCtrlParamTuple(6);
        HTuple model_length2 = m_call_createBlobMatch->GetOutputCtrlParamTuple(7);


        /*if (m_result == 1)
        {
            message += msg[0].S();
            return true;
        }
        else
        {
            message += msg[0].S();
            return false;
        }*/

        HTuple hv_Length;
        TupleLength(model_row, &hv_Length);
        int length = hv_Length[0].I();

        if(length>0)
        {
            m_result = ret[0].I();
            message += msg[0].S();

            m_model_row = model_row[0].D();
            m_model_column = model_column[0].D();
            m_model_phi = model_phi[0].D();
            m_model_length1 = model_length1[0].D();
            m_model_length2 = model_length2[0].D();
        }

        //vector<vector<Point>> vector_vector_contour;

        HObject region = m_call_createBlobMatch->GetOutputIconicParamObject("region");
        m_image_show = Mat::zeros(Size(m_image.cols,m_image.rows),CV_8UC3);
        m_image_show = drawRegionFill(m_image_show, region, Scalar(0, 255, 0));
       

        QImage q_image = matToQImage(m_image_show);
        m_view->setImage(q_image, message);
        m_view->setRectPose(row1, column1, row2, column2);
        /*m_view->m_graphics_shape_rect->setPos(0, 0);
        m_view->m_graphics_shape_rect->m_point1->setPos(column1, row1);
        m_view->m_graphics_shape_rect->m_point2->setPos(column2, row2);
        m_view->m_graphics_shape_rect->update();*/

        emit saveImage();
        return ;
    }
    catch (HDevEngineException& hdev_exception)
    {
        HDevEngine().SetHDevOperatorImpl(NULL);

        QMessageBox::information(nullptr, m_path_para, hdev_exception.Message());
    }

   
    return ;
}

bool FunctionBlobMatch::detection( HObject image_in,QString& message)
{
    if (m_image.empty())
    {
        message += "检测图像为空 ";
        return false;
    }
    try
    {
        

        // Set the input parameters of the procedure
        m_call_blobMatch->SetInputIconicParamObject(1, image_in);
        m_call_blobMatch->SetInputCtrlParamTuple("row1", row1);
        m_call_blobMatch->SetInputCtrlParamTuple("column1", column1);
        m_call_blobMatch->SetInputCtrlParamTuple("row2", row2);
        m_call_blobMatch->SetInputCtrlParamTuple("column2", column2);
        m_call_blobMatch->SetInputCtrlParamTuple("threshold_low", m_qspinbox_threshold_low->value());
        m_call_blobMatch->SetInputCtrlParamTuple("threshold_high", m_qspinbox_threshold_high->value());
        m_call_blobMatch->SetInputCtrlParamTuple("area_low", m_qspinbox_area_low->value());
        m_call_blobMatch->SetInputCtrlParamTuple("area_high", m_qspinbox_area_high->value());
        m_call_blobMatch->SetInputCtrlParamTuple("height_low", m_qspinbox_height_low->value());
        m_call_blobMatch->SetInputCtrlParamTuple("height_high", m_qspinbox_height_high->value());
		m_call_blobMatch->SetInputCtrlParamTuple("width_low", m_qspinbox_width_low->value());
		m_call_blobMatch->SetInputCtrlParamTuple("width_high", m_qspinbox_width_high->value());
		m_call_blobMatch->SetInputCtrlParamTuple("mask_column1", m_qspinbox_mask_column1->value());
		m_call_blobMatch->SetInputCtrlParamTuple("mask_column2", m_qspinbox_mask_column2->value());
        m_call_blobMatch->SetInputCtrlParamTuple("closing_height", m_qspinbox_closing_height->value());
        m_call_blobMatch->SetInputCtrlParamTuple("model_row", m_model_row);
        m_call_blobMatch->SetInputCtrlParamTuple("model_column", m_model_column);
        m_call_blobMatch->SetInputCtrlParamTuple("model_phi", m_model_phi);
        m_call_blobMatch->SetInputCtrlParamTuple("first_black", first_black);
        m_call_blobMatch->SetInputCtrlParamTuple("first_black_erosion_x", 1);
        m_call_blobMatch->SetInputCtrlParamTuple("first_black_erosion_y", m_qspinbox_first_black_erosion->value());
        // Execute the procedure

        //m_engine->StartDebugServer();
        //m_call_createBlobMatch->SetWaitForDebugConnection(true);
        m_call_blobMatch->Execute();
        //m_engine->StopDebugServer();



        // get the output parameters
        HTuple ret = m_call_blobMatch->GetOutputCtrlParamTuple(1);
        HTuple msg = m_call_blobMatch->GetOutputCtrlParamTuple(2);

        

      
        hobject_out = m_call_blobMatch->GetOutputIconicParamObject("image_affine");
        region_out = m_call_blobMatch->GetOutputIconicParamObject("region");
        region_erosion = m_call_blobMatch->GetOutputIconicParamObject("region_erosion");

        m_result = ret[0].I();
        if (m_result == 1)
        {
            message += msg[0].S();
            return true;
        }
        else
        {
            message += msg[0].S();
            return false;
        }
    }
    catch (HDevEngineException& hdev_exception)
    {
        HDevEngine().SetHDevOperatorImpl(NULL);
//        message = hdev_exception.Message();
        message = "没有检测到电极";
        hobject_out = image_in;
        return false;
    }
    return true;
}

void FunctionBlobMatch::detectionImage()
{
    if(m_image.empty())
    {
        //QMessageBox::information(nullptr,"","没有图像");
        return;
    }

    QString message;
	HObject image = matToHImage(m_image);
	bool state = detection(image, message);
	if (!state)
	{
		message += "电极定位错误";
	}
	else
	{
		message += "电极定位OK";
	}
    //detection(m_image.clone(),message);

    Mat image_show = himageToMat(hobject_out);
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);
    m_view->setImage(q_image, message);
  
}

void FunctionBlobMatch::draw( Mat image_in,Mat& image_out)
{
    if(image_in.empty())
        return;
   
    image_out = drawRegion(image_in, region_out, Scalar(0, 255, 0));
	image_out = drawRegion(image_out, region_erosion, Scalar(0, 0, 0));
	

    GenEmptyRegion(&region_out);
	GenEmptyRegion(&region_erosion);
    //rectangle(image_out, Rect(column1,row1,column2-column1,row2-row1), Scalar(0, 255, 0), 1);

}

void FunctionBlobMatch::loadParameter(QString path_in,QString section)
{
    m_path_para = path_in;
    m_section   = section;
    try
    {
        

        first_black = m_qsettings->value(section + "/" + "first_black",0).toInt();

        m_qcombobox_first_black->setCurrentIndex(first_black);

        m_qspinbox_threshold_low->setValue(m_qsettings->value(section + "/" + "threshold_low").toInt());
        m_qspinbox_threshold_high->setValue(m_qsettings->value(section + "/" + "threshold_high").toInt());

		

        m_qspinbox_area_low->setValue(m_qsettings->value(section + "/" + "area_low",10).toInt());
        m_qspinbox_area_high->setValue(m_qsettings->value(section + "/" + "area_high",10000).toInt());

        m_qspinbox_height_low->setValue(m_qsettings->value(section + "/" + "height_low",10).toInt());
        m_qspinbox_height_high->setValue(m_qsettings->value(section + "/" + "height_high",100).toInt());

		m_qspinbox_width_low->setValue(m_qsettings->value(section + "/" + "width_low",10).toInt());
		m_qspinbox_width_high->setValue(m_qsettings->value(section + "/" + "width_high",100).toInt());

		m_qspinbox_mask_column1->setValue(m_qsettings->value(section + "/" + "m_qspinbox_mask_column1", 0).toInt());
		m_qspinbox_mask_column2->setValue(m_qsettings->value(section + "/" + "m_qspinbox_mask_column2", 0).toInt());
        m_qspinbox_closing_height->setValue(m_qsettings->value(section + "/" + "m_qspinbox_closing_height", 0).toInt());

        m_qspinbox_first_black_erosion->setValue(m_qsettings->value(section + "/" + "first_black_erosion").toInt());

 
        row1 = m_qsettings->value(section + "/" + "row1",100).toInt();
        column1 = m_qsettings->value(section + "/" + "column1",100).toInt();
        row2 = m_qsettings->value(section + "/" + "row2",200).toInt();
        column2 = m_qsettings->value(section + "/" + "column2",200).toInt();

        m_view->setRectPose(row1, column1, row2, column2);

    }
    catch(exception& error)
    {
        //QMessageBox::critical(nullptr,error.what() , m_section + "参数需要重新设置");
    }

    return ;
}

void FunctionBlobMatch::saveParameter()
{
    try
    {

        first_black    = m_qcombobox_first_black->currentIndex();

         m_qsettings->setValue(m_section + "/" + "first_black", first_black);

        QRectF qrectf = m_view->m_graphics_shape_rect->boundingRect();
        row1 = m_view->m_graphics_shape_rect->pos().y() + (int)qrectf.y();
        column1 = m_view->m_graphics_shape_rect->pos().x() + (int)qrectf.x();
        row2 = m_view->m_graphics_shape_rect->pos().y() + (int)(qrectf.y() + qrectf.height());
        column2 = m_view->m_graphics_shape_rect->pos().x() + (int)(qrectf.x() + qrectf.width());

        m_qsettings->setValue(m_section + "/" + "row1", row1);
        m_qsettings->setValue(m_section + "/" + "column1", column1);
        m_qsettings->setValue(m_section + "/" + "row2", row2);
        m_qsettings->setValue(m_section + "/" + "column2", column2);

        m_qsettings->setValue(m_section + "/" + "threshold_low", m_qspinbox_threshold_low->value());
        m_qsettings->setValue(m_section + "/" + "threshold_high", m_qspinbox_threshold_high->value());
        m_qsettings->setValue(m_section + "/" + "area_low", m_qspinbox_area_low->value());
        m_qsettings->setValue(m_section + "/" + "area_high", m_qspinbox_area_high->value());
        m_qsettings->setValue(m_section + "/" + "height_low", m_qspinbox_height_low->value());
        m_qsettings->setValue(m_section + "/" + "height_high", m_qspinbox_height_high->value());
		m_qsettings->setValue(m_section + "/" + "width_low", m_qspinbox_width_low->value());
		m_qsettings->setValue(m_section + "/" + "width_high", m_qspinbox_width_high->value());

		m_qsettings->setValue(m_section + "/" + "m_qspinbox_mask_column1", m_qspinbox_mask_column1->value());
		m_qsettings->setValue(m_section + "/" + "m_qspinbox_mask_column2", m_qspinbox_mask_column2->value());
        m_qsettings->setValue(m_section + "/" + "m_qspinbox_closing_height", m_qspinbox_closing_height->value());

        m_qsettings->setValue(m_section + "/" + "first_black_erosion", m_qspinbox_first_black_erosion->value());

    }
    catch(exception& error)
    {
        //QMessageBox::critical(nullptr,error.what() , m_section + "参数需要重新设置");
    }
}

bool FunctionBlobMatch::showDialog()
{
    QImage q_image = matToQImage(m_image);
    m_view->setImage(q_image, "");
    return true;
}

void FunctionBlobMatch::testImage()
{
    QString message;

    if(m_image.empty())
    {
        message += "创建Blob匹配模板失败，模板图像为空 ";
        //QMessageBox::information(nullptr, "", message);
        return ;
    }
    m_image_show = m_image.clone();
    try
    {
        HObject image = matToHImage(m_image_show);

        // Set the input parameters of the procedure
        m_call_createBlobMatch->SetInputIconicParamObject(1, image);
        m_call_createBlobMatch->SetInputCtrlParamTuple("row1", row1);
        m_call_createBlobMatch->SetInputCtrlParamTuple("column1", column1);
        m_call_createBlobMatch->SetInputCtrlParamTuple("row2", row2);
        m_call_createBlobMatch->SetInputCtrlParamTuple("column2", column2);
        m_call_createBlobMatch->SetInputCtrlParamTuple("threshold_low", m_qspinbox_threshold_low->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("threshold_high",  m_qspinbox_threshold_high->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("area_low",  m_qspinbox_area_low->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("area_high",m_qspinbox_area_high->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("height_low",  m_qspinbox_height_low->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("height_high",m_qspinbox_height_high->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("width_low", m_qspinbox_width_low->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("width_high", m_qspinbox_width_high->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("mask_column1", m_qspinbox_mask_column1->value());
		m_call_createBlobMatch->SetInputCtrlParamTuple("mask_column2", m_qspinbox_mask_column2->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("closing_height", m_qspinbox_closing_height->value());
        m_call_createBlobMatch->SetInputCtrlParamTuple("first_black", first_black);
        m_call_createBlobMatch->SetInputCtrlParamTuple("first_black_erosion_x", 1);

        // Execute the procedure


        //m_call_createBlobMatch->SetWaitForDebugConnection(true);
        m_call_createBlobMatch->Execute();




        // get the output parameters
        HTuple ret = m_call_createBlobMatch->GetOutputCtrlParamTuple(1);
        HTuple msg = m_call_createBlobMatch->GetOutputCtrlParamTuple(2);
        HTuple model_row = m_call_createBlobMatch->GetOutputCtrlParamTuple(3);
        HTuple model_column = m_call_createBlobMatch->GetOutputCtrlParamTuple(4);
        HTuple model_phi = m_call_createBlobMatch->GetOutputCtrlParamTuple(5);
        HTuple model_length1 = m_call_createBlobMatch->GetOutputCtrlParamTuple(6);
        HTuple model_length2 = m_call_createBlobMatch->GetOutputCtrlParamTuple(7);




        HObject region = m_call_createBlobMatch->GetOutputIconicParamObject("region");
        m_image_show = Mat::zeros(Size(m_image.cols,m_image.rows),CV_8UC3);
        m_image_show = drawRegionFill(m_image_show, region, Scalar(0, 255, 0));


        QImage q_image = matToQImage(m_image_show);
        m_view->setImage(q_image, message);
        m_view->setRectPose(row1, column1, row2, column2);
        return ;
    }
    catch (HDevEngineException& hdev_exception)
    {
        HDevEngine().SetHDevOperatorImpl(NULL);
    }
}
